use "/homeKRTK/health_shocks/data_for_main_regs_event_study_20231203.dta", clear
drop if control==1

append using "/homeKRTK/health_shocks/additional_data_for_IV_and_placebo_regs_20240416.dta"
replace treated=1 if d==2
drop if d==1 & control==0

append using "/homeKRTK/health_shocks/additional_data_for_all_regs_controls_d_distrib_as_treated_20250319.dta"




gen NONEMP = 1 if ABS==1
	recode NONEMP (.=0) if ABS==0
	
set seed 12345

keep if d<=6
*keep if d>=3 & d<=6
*keep if NONEMP !=.

gen month_shock = t-12*int(t/12)
	recode month_shock (0=12)
gen month_return = month_shock+d

*keep if treated == 1 | control == 1

forval i=1/8 {
	local j=`i'+8
	rename mean_fq`i'_w w_q`j'
	replace mean_fq`i'_FEj_imp =. if flag_missingFEj_fq`i'==1
	rename mean_fq`i'_FEj_imp FE_q`j'


}
forval i=1/8 {
	local j=8-`i'
	rename mean_lq`i'_w w_q`j'
	replace mean_lq`i'_FEj_imp =. if flag_missingFEj_lq`i'==1
	rename mean_lq`i'_FEj_imp FE_q`j'
}


* reshape *
capture drop FEj_t4 FEj_t5 FEj_t6 FEj_t7 FEj_t8 FEj_t9 FEj_t10 FEj_t11 FEj_t12 
capture drop lnw_restr_t4 lnw_restr_t5 lnw_restr_t6 lnw_restr_t7 lnw_restr_t8 lnw_restr_t9 lnw_restr_t10 lnw_restr_t11 lnw_restr_t12 

reshape long w_q FE_q, i(anon) j(relative_time)


recode teaor1_h1 (-1=99) (.=100)

cap drop date
gen date=t+(relative_time-8)*3
replace date=t+d+(relative_time-9)*3 if relative_time>=8

rename kor kor_orig

rename ev ev_orig
rename t t_orig
rename date t

gen ev=floor((t-1)/12)+2003
merge 1:1 anon t using "/homeProspSSD/Admin3/admin3_alap.dta", nogen keep(master match) keepusing(vallazon1 wtip1 wh1 feor1_h2 kor)
replace wh1=. if wh1>40
rename vallazon1 vallazon
merge m:1 vallazon ev using "/homeKRTK/health_shocks/firmquality.dta", nogen keep(master match)

merge 1:1 anon t using "/homePCIESSD800/Admin3/Output_data/Alap/admin3_passziv_kieg_H1_v1.dta", nogen keepusing(passziv_ho)
replace passziv_ho=0 if passziv_ho==.

by anon, sort: gegen full_d=min(d)
drop if full_d==. //drop anons who are never in our baseline sample

rename t date
rename t_orig t

by anon, sort: gegen full_t=min(t)

gen sl_relevant1_=passziv_ho if date>=full_t & date<=full_t+1
by anon, sort: gegen sl_relevant1=total(sl_relevant1_)
drop sl_relevant1_

drop if d==. //back to baseline+falsification sample
drop if d>6

keep if sl_relevant1>0 | control==1 //sample restriction

cap drop w_q_w
cap drop FE_q_w
winsor2 w_q, cuts(1 99) by(ev)
winsor2 FE_q, cuts(1 99) by(ev)

gen tr_relative_time=treated*relative_time
cap drop rtdum*
tabulate tr_relative_time, generate(rtdum)

forval i=2/8 {
	replace rtdum`i'=rtdum`i'-rtdum1
}

gen after=0
replace after=1 if relative_time>=9 & relative_time<=12
replace after=2 if relative_time>=13 & relative_time<=16

xtset anon relative_time


***Baseline regression - input for Figures 3 & 9 - saved in "$folder\EventStudy_Marc2025.xlsx"
cap erase /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log
log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

xtreg w_q i.date i.relative_time rtdum2-rtdum16 if ev_orig>=2012 & letszam~=. & FE_q~=., fe rob
lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8
xtreg FE_q i.date i.relative_time rtdum2-rtdum16 if ev_orig>=2012 & letszam~=. & w_q~=., fe rob
lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8

log close

//baseline average effect - input for Table 2
sum FE_q if ev_orig>=2012 & letszam~=. & w_q~=. & after==0 & control==1

log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

xtreg w_q i.date treated##ib0.after if ev_orig>=2012 & letszam~=. & FE_q~=., fe rob
xtreg FE_q i.date treated##ib0.after if ev_orig>=2012 & letszam~=. & w_q~=., fe rob

log close

***Descriptive plot - input for Figure 1 - saved in "$folder\EventStudy_Marc2025.xlsx"

log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

table relative_time treated if ev_orig>=2012 & letszam~=. & FE_q~=., stat(mean w_q)

log close

***This is for comparison to the futures regressions - input for Figures A3 & B6 - saved in "$folder\EventStudy_Marc2025.xlsx"
log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

xtreg w_q i.date i.relative_time rtdum2-rtdum16 if ev_orig>=2012 & ev_orig<=2014 & letszam~=. & FE_q~=., fe rob
lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8
xtreg FE_q i.date i.relative_time rtdum2-rtdum16 if ev_orig>=2010 & ev_orig<=2014 & letszam~=. & w_q~=., fe rob
lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8

log close

***Hours - input for Figure 4 - saved in "$folder\EventStudy_Marc2025.xlsx"
cap drop log_hours
gen log_hours=log(wh1)

log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

xtreg wh1 i.date i.relative_time rtdum2-rtdum16 if ev_orig>=2012 & letszam~=. & w_q~=., fe rob
lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8
xtreg log_hours i.date i.relative_time rtdum2-rtdum16 if ev_orig>=2012 & letszam~=. & w_q~=., fe rob
lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8

log close

***Sun-Abraham - input for Figures A1 & B4 - saved in "$folder\EventStudy_Marc2025.xlsx"

preserve

keep if ev_orig>=2012 & letszam~=.

cap drop no_adj_rtdum*
tabulate relative_time, generate(no_adj_rtdum) 

keep t treated no_adj_rtdum* w_q_w FE_q_w anon date control relative_time d

cap drop tmod
gen tmod=t if treated==1

log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

eventstudyinteract w_q no_adj_rtdum2-no_adj_rtdum16 if FE_q~=., vce(rob) absorb(anon date) cohort(tmod) control_cohort(control) 
eventstudyinteract FE_q no_adj_rtdum2-no_adj_rtdum16 if w_q~=., vce(rob) absorb(anon date) cohort(tmod) control_cohort(control) 

log close

restore

log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

xtreg w_q i.date i.relative_time##treated if ev_orig>=2012 & letszam~=. & w_q~=., fe rob
xtreg FE_q i.date i.relative_time##treated if ev_orig>=2012 & letszam~=. & w_q~=., fe rob

log close

***Occupation FE as outcome - input for Table D1

merge m:1 feor1_h2 using "/homeKRTK/health_shocks/AKM-FEs/FEo_o2firm.dta", nogen keep(master match)
merge m:1 feor1_h2 using "/homeKRTK/health_shocks/AKM-FEs/FEo_o2.dta", nogen keep(master match)

log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

xtreg occ_fix_o2 i.date ib0.after##treated if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob
xtreg occ_fix_o2firm i.date ib0.after##treated if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob


log close

sum occ_fix_o2 occ_fix_o2firm if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=. & after==0 & control==1

***ONET indicators

	
rename t t_orig
rename date t

merge 1:1 anon t using "/homeProspSSD/Admin3/admin3_feor.dta", nogen keep(master match) keepusing(feor1_h1 feor1_2003_4 feor1_2008_4)

rename t date
rename t_orig t

cap drop whitecoll bluecoll
gen whitecoll = 1 if feor1_h1>=1 & feor1_h1<=4
gen bluecoll = 1 if feor1_h1>=5 & feor1_h1<=9
	
rename feor1_2003_4 feor93
rename feor1_2008_4 feor08

merge m:1 feor93 using "/homeKRTK/health_shocks/ONET/onet_indices_feor93.dta", nogen keep(master match) keepusing(std_physical_narrow std_stress_narrow)
foreach var of varlist std_physical_narrow std_stress_narrow {
	rename `var' `var'_93
}

merge m:1 feor08 using "/homeKRTK/health_shocks/ONET/onet_indices_feor08.dta", nogen keep(master match) keepusing(std_physical_narrow  std_stress_narrow)
foreach var of varlist std_physical_narrow std_stress_narrow {
	replace `var'=`var'_93 if `var'==. & `var'_93~=.
	drop `var'_93
}

***Input for Figure 5 - saved in "$folder\EventStudy_Marc2025.xlsx"

log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

foreach var of varlist std_physical_narrow std_stress_narrow {
	
	xtreg `var' i.date i.relative_time rtdum2-rtdum16  if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob
	lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8
	xtreg `var' i.date i.relative_time rtdum2-rtdum16 if whitecoll==1 & ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob
	lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8
	xtreg `var' i.date i.relative_time rtdum2-rtdum16 if bluecoll==1 & ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob
	lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8
	
}

log close

*** Controls restriction: no co-workers - input for Figures A4 & B7 - saved in "$folder\EventStudy_Marc2025.xlsx"

gen anon_control=anon if control==1 
by vallazon, sort: gegen count_control=nvals(anon_control) //count unique control individuals
cap drop treated_ever
by vallazon, sort: gegen treated_ever=max(treated) //flag firms with a treated worker ever
replace treated_ever=. if vallazon==. | letszam>250 | letszam==. //exclude co-worker controls only if firm ID is not missing and there are at most 250 workers based on the letszam variable

cap drop control_restr
gen control_restr=control
replace control_restr=0 if control==1 & treated_ever==1


log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

xtreg w_q i.date i.relative_time rtdum2-rtdum16 if ev_orig>=2012 & (control_restr==1 | treated==1) & letszam~=. & w_q~=. & FE_q~=., fe rob
lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8
xtreg FE_q i.date i.relative_time rtdum2-rtdum16  if ev_orig>=2012 & (control_restr==1 | treated==1) & letszam~=. & w_q~=. & FE_q~=., fe rob
lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8

log close

*** Within firm matching - input for Figure A4 - saved in "$folder\EventStudy_Marc2025.xlsx"

gen korcsop=floor(kor/5)
cap drop count_treated
by vallazon_t ferfi korcsop, sort: gegen count_treated=total(treated)

cap drop control_restr
gen control_restr=1 if control==1 & count_treated~=0

gen stay=0
replace stay=1 if vallazon==vallazon_t & vallazon~=.
foreach var of varlist stay {
	gen pre`var' = `var' if relative_time<=7
	egen tot_pre`var' = total(pre`var'), by(anon)
}

xtset anon relative_time
log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

xtreg w_q i.date i.relative_time rtdum2-rtdum16 if ev_orig>=2012 & (control_restr==1 | treated==1) & letszam~=. & w_q~=. & FE_q~=. & vallazon==vallazon_t, fe rob 
lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8

log close



***Callaway, SantAnna - input for Figures A2 & B5 - saved in "$folder\EventStudy_Marc2025.xlsx"

cap drop relative_time_mod
gen relative_time_mod=relative_time-8
replace relative_time_mod=relative_time-8-1 if relative_time_mod>7

cap drop tmod
gen tmod=floor(t/3) 

cap drop datemod
gen datemod=tmod+relative_time_mod
replace datemod=datemod+floor(d/3) if relative_time>=8

replace tmod=0 if treated==0

preserve

keep if ev_orig>=2012 & letszam~=. & FE_q~=. & w_q~=.
keep w_q FE_q tmod relative_time_mod anon datemod

gcollapse (mean) w_q FE_q, by(anon datemod tmod)

log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

csdid w_q, ivar(anon) time(datemod) gvar(tmod)
estat event
csdid FE_q, ivar(anon) time(datemod) gvar(tmod)
estat event

log close

restore

***************************Heterogeneities - Input for Figures 6 & 7 - saved in "$folder\EventStudy_Marc2025.xlsx"*******************************


* accident type & body part

gen st=tmerge_t
merge m:1 anon st using "/homeKRTK/health_shocks/bnoSext_code_list.dta", nogen keep(master match)

gen BP=.
replace 	BP = 	7	if 	BNOS03	==	1
replace 	BP = 	7	if 	BNOS13	==	1
replace 	BP = 	7	if 	BNOS23	==	1
replace 	BP = 	7	if 	BNOS33	==	1
replace 	BP = 	1	if 	BNOS40	==	1
replace 	BP = 	1	if 	BNOS41	==	1
replace 	BP = 	1	if 	BNOS42	==	1
replace 	BP = 	1	if 	BNOS43	==	1
replace 	BP = 	1	if 	BNOS44	==	1
replace 	BP = 	1	if 	BNOS45	==	1
replace 	BP = 	1	if 	BNOS46	==	1
replace 	BP = 	1	if 	BNOS49	==	1
replace 	BP = 	2	if 	BNOS50	==	1
replace 	BP = 	2	if 	BNOS51	==	1
replace 	BP = 	2	if 	BNOS52	==	1
replace 	BP = 	2	if 	BNOS53	==	1
replace 	BP = 	2	if 	BNOS54	==	1
replace 	BP = 	2	if 	BNOS55	==	1
replace 	BP = 	2	if 	BNOS56	==	1
replace 	BP = 	2	if 	BNOS59	==	1
replace 	BP = 	3	if 	BNOS60	==	1
replace 	BP = 	3	if 	BNOS61	==	1
replace 	BP = 	3	if 	BNOS62	==	1
replace 	BP = 	3	if 	BNOS63	==	1
replace 	BP = 	3	if 	BNOS64	==	1
replace 	BP = 	3	if 	BNOS65	==	1
replace 	BP = 	3	if 	BNOS66	==	1
replace 	BP = 	3	if 	BNOS69	==	1
replace 	BP = 	4	if 	BNOS70	==	1
replace 	BP = 	4	if 	BNOS71	==	1
replace 	BP = 	4	if 	BNOS72	==	1
replace 	BP = 	4	if 	BNOS73	==	1
replace 	BP = 	4	if 	BNOS74	==	1
replace 	BP = 	4	if 	BNOS75	==	1
replace 	BP = 	4	if 	BNOS76	==	1
replace 	BP = 	4	if 	BNOS79	==	1
replace 	BP = 	5	if 	BNOS80	==	1
replace 	BP = 	5	if 	BNOS81	==	1
replace 	BP = 	5	if 	BNOS82	==	1
replace 	BP = 	5	if 	BNOS83	==	1
replace 	BP = 	5	if 	BNOS84	==	1
replace 	BP = 	5	if 	BNOS85	==	1
replace 	BP = 	5	if 	BNOS86	==	1
replace 	BP = 	5	if 	BNOS89	==	1
replace 	BP = 	6	if 	BNOS90	==	1
replace 	BP = 	6	if 	BNOS91	==	1
replace 	BP = 	6	if 	BNOS92	==	1
replace 	BP = 	6	if 	BNOS93	==	1
replace 	BP = 	6	if 	BNOS94	==	1
replace 	BP = 	6	if 	BNOS95	==	1
replace 	BP = 	6	if 	BNOS96	==	1
replace 	BP = 	6	if 	BNOS99	==	1

gen acc_type=.
replace 	acc_type=	4	if 	BNOS03	==	1
replace 	acc_type=	4	if 	BNOS13	==	1
replace 	acc_type=	4	if 	BNOS23	==	1
replace 	acc_type=	4	if 	BNOS33	==	1
replace 	acc_type=	1	if 	BNOS40	==	1
replace 	acc_type=	2	if 	BNOS41	==	1
replace 	acc_type=	3	if 	BNOS42	==	1
replace 	acc_type=	4	if 	BNOS43	==	1
replace 	acc_type=	1	if 	BNOS44	==	1
replace 	acc_type=	1	if 	BNOS45	==	1
replace 	acc_type=	1	if 	BNOS46	==	1
replace 	acc_type=	1	if 	BNOS49	==	1
replace 	acc_type=	1	if 	BNOS50	==	1
replace 	acc_type=	2	if 	BNOS51	==	1
replace 	acc_type=	3	if 	BNOS52	==	1
replace 	acc_type=	4	if 	BNOS53	==	1
replace 	acc_type=	1	if 	BNOS54	==	1
replace 	acc_type=	1	if 	BNOS55	==	1
replace 	acc_type=	1	if 	BNOS56	==	1
replace 	acc_type=	1	if 	BNOS59	==	1
replace 	acc_type=	1	if 	BNOS60	==	1
replace 	acc_type=	2	if 	BNOS61	==	1
replace 	acc_type=	3	if 	BNOS62	==	1
replace 	acc_type=	4	if 	BNOS63	==	1
replace 	acc_type=	1	if 	BNOS64	==	1
replace 	acc_type=	1	if 	BNOS65	==	1
replace 	acc_type=	1	if 	BNOS66	==	1
replace 	acc_type=	1	if 	BNOS69	==	1
replace 	acc_type=	1	if 	BNOS70	==	1
replace 	acc_type=	2	if 	BNOS71	==	1
replace 	acc_type=	3	if 	BNOS72	==	1
replace 	acc_type=	4	if 	BNOS73	==	1
replace 	acc_type=	1	if 	BNOS74	==	1
replace 	acc_type=	1	if 	BNOS75	==	1
replace 	acc_type=	1	if 	BNOS76	==	1
replace 	acc_type=	1	if 	BNOS79	==	1
replace 	acc_type=	1	if 	BNOS80	==	1
replace 	acc_type=	2	if 	BNOS81	==	1
replace 	acc_type=	3	if 	BNOS82	==	1
replace 	acc_type=	4	if 	BNOS83	==	1
replace 	acc_type=	1	if 	BNOS84	==	1
replace 	acc_type=	1	if 	BNOS85	==	1
replace 	acc_type=	1	if 	BNOS86	==	1
replace 	acc_type=	1	if 	BNOS89	==	1
replace 	acc_type=	1	if 	BNOS90	==	1
replace 	acc_type=	2	if 	BNOS91	==	1
replace 	acc_type=	3	if 	BNOS92	==	1
replace 	acc_type=	4	if 	BNOS93	==	1
replace 	acc_type=	1	if 	BNOS94	==	1
replace 	acc_type=	1	if 	BNOS95	==	1
replace 	acc_type=	1	if 	BNOS96	==	1
replace 	acc_type=	1	if 	BNOS99	==	1

recode acc_ (2=1) (4=2)

label define acc_ 1 "injury/wound" 2 "dislocation/sprain" 3 "fracture"
lab val acc_ acc_
replace acc_type=2 if acc_type==1 //merge first two categories!!!

label define BP 1 "shoulder/upper-arm" 2 "elbow/forearm" 3 "hand/fingers/wrist" 4 "hip/thigh" 5 "knee/lower-leg" 6 "ankle/foot/toes" 7 "head/neck/thorax/spine" 
lab val BP BP

drop BNO*

log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

cap drop after1_*
cap drop after2_*
forval i=2/3 {
	gen after1_`i'=0
	replace after1_`i'=1 if after==1 & acc_type==`i'
	gen after2_`i'=0
	replace after2_`i'=1 if after==2 & acc_type==`i'
}

xtreg w_q i.date i.after after1* after2* if  ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob
xtreg FE_q i.date i.after after1* after2* if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob

cap drop after1_*
cap drop after2_*
forval i=1/6 {
	gen after1_`i'=0
	replace after1_`i'=1 if after==1 & BP==`i'
	gen after2_`i'=0
	replace after2_`i'=1 if after==2 & BP==`i'
}

xtreg w_q i.date i.after after1* after2* if  ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob
xtreg FE_q i.date i.after after1* after2* if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob


* absence length
gen Dgroup = 1 if d==2 | d==3 | d==4
replace Dgroup = 2 if d==5 | d==6

cap drop after1_*
cap drop after2_*
forval i=1/2 {
	gen after1_`i'=0
	replace after1_`i'=1 if after==1 & Dgroup==`i'
	gen after2_`i'=0
	replace after2_`i'=1 if after==2 & Dgroup==`i'
}

xtreg w_q i.date treated##after1* treated##after2* if  ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob
xtreg FE_q i.date treated##after1* treated##after2* if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob

log close


* gen group by pre-firmFE 

cap drop FE_base_dec
egen FE_base_dec=xtile(FEj_t0), nq(2)

cap drop after1_*
cap drop after2_*

forval i=1/2 {
	gen after1_dec`i'=0
	replace after1_dec`i'=1 if after==1 & FE_base_dec==`i'
	gen after2_dec`i'=0
	replace after2_dec`i'=1 if after==2 & FE_base_dec==`i'
}
	
log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

xtreg w_q i.date treated##after1* treated##after2* if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob
xtreg FE_q i.date treated##after1* treated##after2* if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob


log close

* gender


cap drop after1_*
cap drop after2_*
forval i=0/1 {
	gen after1_`i'=0
	replace after1_`i'=1 if after==1 & ferfi==`i'
	gen after2_`i'=0
	replace after2_`i'=1 if after==2 & ferfi==`i'
}
	
log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

xtreg w_q i.date treated##after1* treated##after2* if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob
xtreg FE_q i.date treated##after1* treated##after2* if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob


log close

* tenure

rename t t_
gen t=tmerge_t-1
merge m:1 anon t using "/homeKRTK/health_shocks/tenure_2022Nov14.dta", keep(master match) nogen keepusing(first_firm)
rename first_firm first_firm_l
replace t=tmerge_t
merge m:1 anon t using "/homeKRTK/health_shocks/tenure_2022Nov14.dta", keep(master match) nogen keepusing(first_firm)
drop t 
rename t_ t
gen tenure=first_firm
replace tenure=first_firm_l if flagEMPml1==1

cap drop long_tenure
gen long_tenure=0 if tenure<24
replace long_tenure=1 if tenure>=24 & tenure~=.

cap drop after1_*
cap drop after2_*
forval i=0/1 {
	gen after1_`i'=0
	replace after1_`i'=1 if after==1 & long_tenure==`i'
	gen after2_`i'=0
	replace after2_`i'=1 if after==2 & long_tenure==`i'
}
	
log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

xtreg w_q i.date##long_tenure treated##after1* treated##after2* if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob
xtreg FE_q i.date##long_tenure treated##after1* treated##after2* if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob
log close

* occupation

cap drop whitecoll bluecoll
gen whitecoll = 1 if occupation>=1 & occupation<=4
	recode whitecoll (.=0) if occupation>=5 & occupation<=7
gen bluecoll = 1 if whitecoll==0
	recode bluecoll (.=0)

cap drop after1_*
cap drop after2_*
forval i=0/1 {
	gen after1_`i'=0
	replace after1_`i'=1 if after==1 & bluecoll==`i'
	gen after2_`i'=0
	replace after2_`i'=1 if after==2 & bluecoll==`i'
}
	
log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

xtreg w_q i.date treated##after1* treated##after2* if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob
xtreg FE_q i.date treated##after1* treated##after2* if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob
log close

* age

gen aggr = 1 if (kor>=20 & kor<=30) 
replace aggr = 2 if (kor>=31 & kor<=40)
replace aggr = 3 if (kor>=41 & kor<=50) 

cap drop after1_*
cap drop after2_*
forval i=1/3 {
	gen after1_`i'=0
	replace after1_`i'=1 if after==1 & aggr==`i'
	gen after2_`i'=0
	replace after2_`i'=1 if after==2 & aggr==`i'
}
	
log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

xtreg w_q i.date##aggr treated##after1* treated##after2* if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob
xtreg FE_q i.date##aggr treated##after1* treated##after2* if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=., fe rob
log close

********Participation - Input for Figure B1 - saved in "$folder\EventStudy_Marc2025.xlsx"

rename t t_orig
rename date t

merge 1:1 anon t using "/homeProspSSD/Admin3/admin3_alap.dta", nogen keep(master match) keepusing(fogvisz1 w1)

rename t date
rename t_orig t

rename fogvisz1 fogvisz1_1
rename w1 w1_1


rename t t_orig
rename date t

replace t=t+1
merge 1:1 anon t using "/homeProspSSD/Admin3/admin3_alap.dta", nogen keep(master match) keepusing(fogvisz1 w1)
replace t=t-1

rename t date
rename t_orig t

rename fogvisz1 fogvisz1_2
rename w1 w1_2


rename t t_orig
rename date t

replace t=t+2
merge 1:1 anon t using "/homeProspSSD/Admin3/admin3_alap.dta", nogen keep(master match) keepusing(fogvisz1 w1)
replace t=t-2

rename t date
rename t_orig t

rename fogvisz1 fogvisz1_3
rename w1 w1_3

cap drop w_level
replace w1_1=0 if w1_1<0 | w1_1==.
replace w1_2=0 if w1_2<0 | w1_2==.
replace w1_3=0 if w1_3<0 | w1_3==.
gen w_level=(w1_1+w1_2+w1_3)

cap drop emp*
gen emp1=0
replace emp1=1 if fogvisz1_1~=.
gen emp2=0
replace emp2=1 if fogvisz1_2~=.
gen emp3=0
replace emp3=1 if fogvisz1_3~=.

gen emp=(emp1+emp2+emp3)/3
replace emp=1 if emp>0 & emp~=. //emp at least once in the quarter

log using /homeKRTK/health_shocks/EventStudy/log_DID_Marc2025.log, append

table relative_time treated if ev_orig>=2012, stat(mean emp)

xtreg emp i.date i.relative_time rtdum2-rtdum16 if ev_orig>=2012, fe rob
lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8


log close



